﻿using DapperExtensions;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
using WMLogistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Dapper;
using DapperExtensions;

namespace WMLogistics_Dao
{
    public class ReportBidDao
    {
        private SqlGeneratorImpl sqlGenerator;
        public ReportBidDao()
        {
            var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
            sqlGenerator = new SqlGeneratorImpl(config);
            DapperExtensions.DapperExtensions.Configure(config);
        }

        public int Add(wuliu_report_base report)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return (int)conn.Insert<wuliu_report_base>(report);
            }
        }

        public wuliu_report_base Get(int id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {

                return conn.Get<wuliu_report_base>(id);
            }
        }

        /// <summary>
        /// 获取报表数据
        /// </summary>
        /// <returns></returns>
        public PageData<wuliu_report_base> GetReportList(IPredicate predicate, int pageIndex, int pageSize)
        {
            IList<ISort> sort = new List<ISort>
                                    {
                                        Predicates.Sort<wuliu_report_base>(p => p.bid_id,false)
                                    };
            using (var conn = ConnectionHelper.OpenConnection())
            {
                var pageData = new PageData<wuliu_report_base>();
                pageData.Items = conn.GetPage<wuliu_report_base>(predicate, sort, pageIndex, pageSize);
                pageData.TotalNum = conn.Count<wuliu_report_base>(predicate);
                pageData.CurrentPage = pageIndex;
                pageData.TotalPageCount = pageData.TotalNum % pageSize == 0 ? pageData.TotalNum / pageSize : pageData.TotalNum / pageSize + 1;
                return pageData;
            }
  
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(wuliu_report_base model)
		{
			 using (var conn = ConnectionHelper.OpenConnection())
				 {
					StringBuilder str=new StringBuilder();
					StringBuilder strSql=new StringBuilder();
					strSql.Append("update wuliu_report_base set ");
					if(model.report_start_time.IfNotNull())
						strSql.Append("report_start_time=@report_start_time,");
					if(model.report_end_time.IfNotNull())
						strSql.Append("report_end_time=@report_end_time,");
					if(model.report_time.IfNotNull())
						strSql.Append("report_time=@report_time,");
					if(model.state.IfNotNull())
						strSql.Append("state=@state,");
					if(model.price.IfNotNull())
						strSql.Append("price=@price,");
					if(model.remark.IfNotNull())
						strSql.Append("remark=@remark,");
					if(model.wuliu_id.IfNotNull())
						strSql.Append("wuliu_id=@wuliu_id,");
					if(model.bid_id.IfNotNull())
						strSql.Append("bid_id=@bid_id,");
					if(model.company_id.IfNotNull())
						strSql.Append("company_id=@company_id,");
					if(model.kq_price.IfNotNull())
						strSql.Append("kq_price=@kq_price,");
					if(model.bid_no.IfNotNull())
						strSql.Append("bid_no=@bid_no,");
					if(model.order_num.IfNotNull())
						strSql.Append("order_num=@order_num,");
					if(model.receipt_time.IfNotNull())
						strSql.Append("receipt_time=@receipt_time,");
					if(model.end_city_id.IfNotNull())
						strSql.Append("end_city_id=@end_city_id,");
					if(model.wuliu_no.IfNotNull())
						strSql.Append("wuliu_no=@wuliu_no,");
					if(model.pickup_time.IfNotNull())
						strSql.Append("pickup_time=@pickup_time,");
					if(model.bid_state.IfNotNull())
						strSql.Append("bid_state=@bid_state,");
					if(model.damage.IfNotNull())
						strSql.Append("damage=@damage,");
					if(model.bill_price.IfNotNull())
						strSql.Append("bill_price=@bill_price,");
					if(model.remark2.IfNotNull())
						strSql.Append("remark2=@remark2,");
					if(model.Operator.IfNotNull())
						strSql.Append("operator=@operator,");
					if(model.operator_time.IfNotNull())
						strSql.Append("operator_time=@operator_time,");
					if(model.remark1.IfNotNull())
						strSql.Append("remark1=@remark1,");
					if(model.load_no.IfNotNull())
						strSql.Append("load_no=@load_no,");
					if(model.tax_price.IfNotNull())
						strSql.Append("tax_price=@tax_price,");
					if(model.tax.IfNotNull())
						strSql.Append("tax=@tax,");
					if(model.wuliu_name.IfNotNull())
						strSql.Append("wuliu_name=@wuliu_name,");
					if(model.company_no.IfNotNull())
						strSql.Append("company_no=@company_no,");
					if(model.company_name.IfNotNull())
						strSql.Append("company_name=@company_name,");
					if(model.end_city_id_x.IfNotNull())
						strSql.Append("end_city_id_x=@end_city_id_x,");
					if(model.end_city_name.IfNotNull())
						strSql.Append("end_city_name=@end_city_name");
					str.Append(strSql.ToString().TrimEnd(','));
					str.Append(" where report_id=@report_id ");
					return conn.Execute(str.ToString(), model);
				}
		}

       
    }
}
